package com.sc.nft.admin.rpc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.entity.dto.*;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.ExpenditureRecordTypeEnum;
import com.sc.nft.enums.UserBalanceTypeEnum;
import com.sc.nft.enums.UserEquityPropsGetTypeEnum;
import com.sc.nft.enums.WalletDetailTypeEnum;
import com.sc.nft.service.*;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rpc/mall/payment")
@Api(tags = "商城外部api")
public class MallWithoutApiRpc {
    private final UserWalletService userWalletService;

    private final EquityPropsService equityPropsService;

    private final UserCollectionService userCollectionService;

    private final DigitalCollectionService digitalCollectionService;

    private final DaoIdentityService daoIdentityService;

    private final UserEquityPropsService userEquityPropsService;

    @PostMapping(value = "/getUserBalance", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户余额", notes = "获取用户余额", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result getUserBalance(@RequestBody IdRequest idRequest) {
        return Result.success_ios(userWalletService.getMallUserBalance(idRequest.getId()));
    }

    @PostMapping(value = "/shoppingAllowanceControls", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "购物金操作", notes = "扣款", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result shoppingAllowanceControls(@RequestBody MallDeductMoneyDto mallDeductMoneyDto) {
        if (mallDeductMoneyDto.getIsAdd()) {
            mallDeductMoneyDto.setType(ExpenditureRecordTypeEnum.MALL_SHOPPING_ALLOWANCE_PAY);
            mallDeductMoneyDto.setUserBalanceTypeEnum(UserBalanceTypeEnum.WISH_BALANCE);
            mallDeductMoneyDto.setWalletDetailTypeEnum(WalletDetailTypeEnum.MALL_REFUND);
        } else {
            mallDeductMoneyDto.setType(ExpenditureRecordTypeEnum.WISH_BALANCE_DEDUCTION);
            mallDeductMoneyDto.setUserBalanceTypeEnum(UserBalanceTypeEnum.WISH_BALANCE);
            mallDeductMoneyDto.setWalletDetailTypeEnum(WalletDetailTypeEnum.WISH_USED);
        }
        return Result.success_ios(userWalletService.shoppingAllowanceControls(mallDeductMoneyDto));
    }


    @PostMapping(value = "/propEquityList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "道具卡列表", notes = "道具卡列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<MallEquityPropsVO> propCardList(@RequestBody VistInFlagDTO vist) {
        return PageResult.successIos(equityPropsService.getMallEquityProps(vist));
    }

    @PostMapping(value = "/userEquityList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户道具卡列表", notes = "用户道具卡列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<List<MallEquityPropsVO>> userEquityList(@RequestBody VistInFlagUserDTO vistInFlagUserDTO) {
        return Result.success_ios(equityPropsService.getUserEquityProps(vistInFlagUserDTO));
    }
    
    @PostMapping(value = "/pageUserEquityList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页获取用户道具卡列表", notes = "用户道具卡列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<MallUserEquityPropsVO> pageUserEquityList(@RequestBody VistInFlagUserDTO vistInFlagUserDTO) {
        return PageResult.successIos(userEquityPropsService.pageUserEquityProps(vistInFlagUserDTO));
    }

    @PostMapping(value = "/userEquityIdList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户指定权益道具列表", notes = "用户指定权益道具列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<UserEquityPropsDetailVO> userEquityIdList(@RequestBody UserEquityIdDTO userEquityIdDTO) {
        return PageResult.successIos(equityPropsService.userEquityIdList(userEquityIdDTO));
    }

    @PostMapping(value = "/issuePropEquity", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "发放权益道具", notes = "发放权益道具", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result issuePropEquity(@RequestBody MallIssuePropEquityDTO mallIssuePropEquityDTO) {
        userEquityPropsService.mallIssuePropEquity(mallIssuePropEquityDTO);
        return Result.success_ios(null);
    }


    @PostMapping(value = "/deductPropEquity", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "扣除权益道具", notes = "扣除权益道具", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result deductPropEquity(@RequestBody MallDeductPropEquityDTO mallDeductPropEquityDTO) {
        userEquityPropsService.mallSubtractPropEquity(mallDeductPropEquityDTO);
        return Result.success_ios(null);
    }


    @PostMapping(value = "/pageDaoCollection", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取商户藏品", notes = "获取商户藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<MallCollectionVO> pageDaoCollection(@Valid @RequestBody MallDaoCollectionDTO request) {
        IPage<MallCollectionVO> pageCollection = digitalCollectionService.pageDaoCollection(request);
        return PageResult.successIos(pageCollection);
    }


    @PostMapping(value = "/pageUserCollection", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户藏品", notes = "获取用户藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<MallUserCollectionVO> pageUserCollection(@Valid @RequestBody MallUserCollectionDTO request) {
        IPage<MallUserCollectionVO> mallUserCollectionVOIPage = userCollectionService.pageMallUserCollection(request);
        return PageResult.successIos(mallUserCollectionVOIPage);
    }

    @PostMapping(value = "/pageDaoIdentity", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取商户权益卡", notes = "获取商户藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<MallDaoIdentityVo> getDaoIdentity(@RequestBody MallDaoIdentityDTO request) {
        return PageResult.successIos(daoIdentityService.pageDaoIdentity(request.toMp(), request.getDaoId(), request.getFirstTitle()));
    }


    @PostMapping(value = "/pageUserIdentity", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户权益卡", notes = "获取用户藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<MallUserIdentityVo> getUserIdentity(@Valid @RequestBody MallUserIdentityDTO mallUserIdentityDTO) {
        return PageResult.successIos(daoIdentityService.pageUserIdentity(mallUserIdentityDTO.toMp(), mallUserIdentityDTO.getUserId(), mallUserIdentityDTO.getIdentityIdList()));
    }


    @PostMapping(value = "/pageUserIdByCollectionId", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取藏品持有的用户", notes = "获取藏品持有的用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<MallCollectionUserVO> pageUserIdByCollectionId(@Valid @RequestBody MallCollectionUSerDTO mallCollectionUSerDTO) {
        return PageResult.successIos(userCollectionService.pageUserIdByCollectionId(mallCollectionUSerDTO));
    }


    @PostMapping(value = "/pageEquityHold", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户id持有道具数量", notes = "获取用户id持有道具数量", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<MallEquityUserHoldVo> pageEquityHold(@Valid @RequestBody MallEquityUserHoldDTO mallUserIdentityDTO) {
        return PageResult.successIos(userEquityPropsService.pageEquityHold(mallUserIdentityDTO));
    }

    @PostMapping(value = "/mallPropsGetLog", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取新商城道具获取记录", notes = "获取新商城道具获取记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<MallUserEquityPropsVO> mallPropsGetLog(@Valid @RequestBody MallPropsGetLogRequestDTO requestDTO) {
        return PageResult.successIos(equityPropsService.mallPropsGetLog(requestDTO.getUserId(),
            UserEquityPropsGetTypeEnum.MALL_PRIZE, requestDTO.getPageNo(), requestDTO.getPageSize()));
    }
}
